<script>
	import {
		UserModel
	} from '@/service/model/user.js'
	const userModel = new UserModel()
	export default {
		onLaunch: function() {
			console.warn('当前组件仅支持 uni_modules 目录结构 ，请升级 HBuilderX 到 3.1.0 版本以上！')
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
			// #ifdef MP-WEIXIN
			this.getLogin()
			//#endif
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			getLogin() {
				uni.login({
					success: async function(res) {
						if (res.code) {
							uni.setStorageSync('wxCode', res.code);
							const {
								data,
								success
							} = await userModel.getUserByWX({
								code: res.code
							})
							if (success) {
								//true为未注册,需要进一步调用微信登录按钮授权登录
								if (data.success) {
									uni.setStorageSync('openid', data.data.openid)
									uni.setStorageSync('sessionKey', data.data.sessionKey)
									console.log('获取了openid-sessionKey', data)
								} else {
									//注册过的用户
									uni.setStorageSync('token',data.data)
									console.log('老用户登录成功信息',data)
								}

							}
						}
					},
					fail: function() {
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */

	// 设置整个项目的背景色
	page {
		background-color: white;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
</style>
